﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace HermesWinForm
{
    public partial class FulfilmentScan : Form
    {
        Thread threadImport;

        public FulfilmentScan()
        {
            InitializeComponent();
        }

        private void FulfilmentScan_Load(object sender, EventArgs e)
        {
            txtTrackingNo.Focus();
           
        }

        private void txtTrackingNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnScan_Click(sender,e);
            }
        }

        private void btnScan_Click(object sender, EventArgs e)
        {
            string trackingNo = txtTrackingNo.Text.Trim();

            OrdersServiceReference.SalesOrderSoapClient orderClient = new OrdersServiceReference.SalesOrderSoapClient();

            //richtxtMessage.Text = string.Empty;
            btnScan.Enabled = false;
            
            string displayMessage = "";
            

            threadImport = new Thread(delegate()
            {
                    StringBuilder sbMessage = new StringBuilder();

                    try
                    {
                        string message = orderClient.FulfilmentScan(trackingNo,"liutao");
                      
                        if (this.InvokeRequired)
                        {
                            this.Invoke(new MethodInvoker(delegate()
                            {
                                if (message == "0")
                                {
                                    displayMessage = trackingNo + ": Scan Success!";
                                    txtTrackingNo.Text = "";
                                    txtTrackingNo.SelectAll();
                                    
                                }
                                else
                                {
                                    displayMessage = trackingNo + ": " + message;
                                    txtTrackingNo.SelectAll();
                                }
                                displayMessage += "\n\n";
                                richtxtMessage.Text = displayMessage + richtxtMessage.Text;
                            }));
                        }
                    }
                    catch (System.Exception ex)
                    {
                        if (this.InvokeRequired)
                        {
                            this.Invoke(new MethodInvoker(delegate()
                            {
                               
                                displayMessage = trackingNo + ": " + ex.Message;
                                displayMessage += "\n\n";
                                richtxtMessage.Text = displayMessage + richtxtMessage.Text;
                                txtTrackingNo.SelectAll();
                            }));
                        }
                    }

            });

            threadImport.Start();
            btnScan.Enabled = true;
        }
    }
}
